/**
 * 
 */
package org.cuatrovientos.erp.controller;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.ProductTypeBOInterface;
import org.cuatrovientos.erp.model.ProductType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;



/**
 * Controller class form Product Type management
 * @author Natalia Bernarte
 */
@Controller
public class ProductTypeController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(ProductTypeController.class);

	@Autowired
    private ProductTypeBOInterface productTypeBO;
    
        /**
         * default constructor
         */
        public ProductTypeController () {
               logger.info("ProductType BEAN instantiated");
                
        }
        
        
        
        /**
         * handles default /productType or /productType/list request
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/productType","/productType/list"})
         */
        @RequestMapping({"/productType","*","/productType/list"})
        public ModelAndView productTypeDefault() {
        	logger.info("default");
 
                Map<String, Object> model = new HashMap<String, Object>();

                List<ProductType> productTypeList = productTypeBO.selectAll();
                model.put("productTypes", productTypeList );
                return new ModelAndView("productTypeList", model);     
        }

        /**
         * handles /productType/search request, redirected to default page
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/productType/search"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/productType/search"})
        public ModelAndView search(@RequestParam(value="productType", defaultValue="", required=true) String description) {
        	logger.info("search");
 
            Map<String, Object> model = new HashMap<String, Object>();

            List<ProductType> productTypeList = productTypeBO.search(description);
            model.put("productTypes", productTypeList );
            
            return new ModelAndView("productTypeList", model);     
        }

        /**
         * handles delete productType, and redirect to productType default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/productType","/productType/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/productType/delete"})
        public String deleteProductType(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("delete " + id);

        	productTypeBO.deleteById(id);
            return "redirect:/productType";

        }
        
        /**
         * handles new productType request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it: in this case "productType"
         * @return the name of the view to show
         * @RequestMapping({"/productType/new"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/productType/new"})
        public ModelAndView newProductType() {
        		logger.info("new delegation ");
                ProductType productType = new ProductType();
               
                return new ModelAndView("NewProductType", "productType", productType);
        }
        
        /**
         * handles new productType request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/productType/new"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/productType/save"})
        public String newDelegation(@Valid ProductType productType, BindingResult bindingResult) {
        		logger.info("new productType " + productType.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("productType", productType);
        			return "NewProductType";
        		}
        		
        		// If data is ok, insert and go on
        		productTypeBO.insert(productType);
                return "redirect:/productType";
        }
        
        /**
         * handles delete productType, and redirect to productType default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/productType","/productType/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/productType/detail"})
        public ModelAndView productTypeDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("Detail " + id);
            Map<String,Object> model = new HashMap<String,Object>();
            ProductType productType = productTypeBO.selectById(id);
            model.put("productType", productType);
            
            return new ModelAndView("ProductTypeDetail",model);

        }
        
        /**
         * handles productType update, first part: register loaded
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/productType/update"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/productType/update"})
        public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("update " + id);
                Map<String,Object> model = new HashMap<String,Object>();
                ProductType productType = productTypeBO.selectById(id);
                model.put("productType", productType);
               return new ModelAndView("UpdateProductType",model);

        }
        
        /**
         * handles save update productType request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/productType/saveUpdate"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/productType/saveupdate"})
        public String saveUpdate(@Valid ProductType productType, BindingResult bindingResult) {
        		logger.info("update productType " + productType.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("productType", productType);
        			return "UpdateProductType";
        		}
        		
        		productTypeBO.update(productType);
                return "redirect:/productType";
        }
}
